package com.o2opuzi.lib.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * XML解析类
 * @author zhou qi
 *
 */
public class XmlUtils {

	/**
	 * 把XML字符串转换为map对象
	 * @param xml
	 * @return
	 * @throws Exception 
	 */
    @SuppressWarnings("rawtypes")
	public static Map<String, String> xmlToMap(String xml) throws Exception {
    	Map<String, String> map=new HashMap<String, String>();;
        Document doc = null;
        int num=0;
        try {
            doc = DocumentHelper.parseText(xml);
        	Element employees=doc.getRootElement();   
	        	for(Iterator j = employees.elementIterator(); j.hasNext();){   
		        	Element node=(Element) j.next();  
		        	if(map.size()>0 && null!=map.get(node.getName())){
		        		map.put(node.getName()+String.valueOf(num), node.getText());
		        	}else{
		        		map.put(node.getName(), node.getText());
		        	}
	        	num++;
        	}   
        } catch (Exception e) {
        	throw e;
        }
        return map;
    }
}
